//
// Copyright 2021 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// stylelint-disable selector-class-pattern --
// Selector '.mdc-*' should only be used in this project.

@use 'sass:map';
@use 'sass:math';
@use '@material/feature-targeting/feature-targeting';
@use '@material/theme/custom-properties';
@use '@material/theme/keys';
@use '@material/theme/state';
@use '@material/theme/theme';
@use '@material/theme/theme-color';
@use '@material/tokens/resolvers';
@use '@material/ripple/ripple-theme';
@use './button-base';
@use './button-shared-theme';
@use './button-ripple';

$outlined-border-width: 1px !default;
$outline-color: rgba(theme-color.prop-value(on-surface), 0.12) !default;

$custom-property-prefix: 'outlined-button';

$light-theme: (
  container-height: button-shared-theme.$height,
  container-shape: button-shared-theme.$shape-radius,
  disabled-label-text-color: button-shared-theme.$disabled-ink-color,
  disabled-outline-color: button-shared-theme.$disabled-container-color,
  focus-label-text-color: null,
  focus-outline-color: null,
  focus-state-layer-color: primary,
  focus-state-layer-opacity: 0.12,
  hover-label-text-color: null,
  hover-outline-color: null,
  hover-state-layer-color: primary,
  hover-state-layer-opacity: 0.04,
  /// Prevent the increased touch target from being reseted if the
  /// container-height differs from the default (36px)
  keep-touch-target: false,
  label-text-color: primary,
  label-text-font: button-font-family,
  label-text-size: button-font-size,
  label-text-tracking: button-letter-spacing,
  label-text-transform: button-text-transform,
  label-text-weight: button-font-weight,
  outline-color: $outline-color,
  outline-width: $outlined-border-width,
  pressed-label-text-color: null,
  pressed-outline-color: null,
  pressed-state-layer-color: primary,
  pressed-state-layer-opacity: 0.12,
  with-icon-icon-color: null,
  with-icon-icon-size: 18px,
  with-icon-hover-icon-color: null,
  with-icon-focus-icon-color: null,
  with-icon-pressed-icon-color: null,
  with-icon-disabled-icon-color: null,
  focus-ring-color: null,
  focus-ring-offset: 0,
);

/// Sets theme based on provided theme configuration.
/// Only emits theme related styles.
/// @param {Map} $theme - Theme configuration to use.
@mixin theme($theme, $resolver: resolvers.$material) {
  @include theme.validate-theme($light-theme, $theme);
  $theme: button-shared-theme.resolve-theme-elevation-keys(
    $theme,
    $resolver: $resolver
  );
  @include keys.declare-custom-properties($theme, $custom-property-prefix);
}

@mixin theme-styles(
  $theme,
  $resolver: resolvers.$material,
  $query: feature-targeting.all()
) {
  @include theme.validate-theme-styles($light-theme, $theme);
  $theme: keys.create-theme-properties(
    $theme,
    $prefix: $custom-property-prefix
  );
  @include _theme($theme, $resolver, $query: $query);
}

@mixin _theme($theme, $resolver, $query) {
  @include button-shared-theme.theme-styles($theme, $resolver, $query: $query);
  @include outline-color(
    (
      default: map.get($theme, outline-color),
      disabled: map.get($theme, disabled-outline-color),
      focus: map.get($theme, focus-outline-color),
      hover: map.get($theme, hover-outline-color),
      pressed: map.get($theme, pressed-outline-color),
    ),
    $query: $query
  );
  @include outline-width(map.get($theme, outline-width), $query: $query);
}

@mixin deprecated-theme-styles($query: feature-targeting.all()) {
  .mdc-button--outlined {
    $theme: map.merge(
      $light-theme,
      (
        focus-state-layer-color: null,
        focus-state-layer-opacity: null,
        hover-state-layer-color: null,
        hover-state-layer-opacity: null,
        pressed-state-layer-color: null,
        pressed-state-layer-opacity: null,
        label-text-font: null,
        label-text-size: null,
        label-text-tracking: null,
        label-text-transform: null,
        label-text-weight: null,
      )
    );
    @include _theme($theme, resolvers.$material, $query: $query);
  }
}

///
/// Sets the outline color to the given color for an enabled button.
/// @param {Color} $color-or-map - The desired outline color, specified either
///     as a flat value or a map of colors with states
///     {default, hover, focused, pressed, disabled} as keys.
///
@mixin outline-color($color-or-map, $query: feature-targeting.all()) {
  &:not(:disabled) {
    @include _outline-color(
      state.get-default-state($color-or-map),
      $query: $query
    );

    &:hover {
      @include _outline-color(
        state.get-hover-state($color-or-map),
        $query: $query
      );
    }

    @include ripple-theme.focus() {
      @include _outline-color(
        state.get-focus-state($color-or-map),
        $query: $query
      );
    }

    // Increase active state specificity due to ripple-theme.focus().
    &:active,
    &:focus:active {
      @include _outline-color(
        state.get-pressed-state($color-or-map),
        $query: $query
      );
    }
  }

  &:disabled {
    @include _outline-color(
      state.get-disabled-state($color-or-map),
      $query: $query
    );
  }
}

///
/// Sets the outline color to the given color for a disabled button.
/// @param {Color} $color - The desired outline color.
/// @deprecated - call `outline-color` instead with `disabled` as a map key.
///
@mixin disabled-outline-color($color, $query: feature-targeting.all()) {
  @include outline-color(
    (
      disabled: $color,
    ),
    $query: $query
  );
}

@mixin outline-width(
  $outline-width,
  $padding: button-shared-theme.$contained-horizontal-padding,
  // For a button with an icon, the padding on the side of the button with the
  // icon.
  $padding-icon: button-shared-theme.$contained-horizontal-padding-icon,
  $query: feature-targeting.all()
) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @if $outline-width != null {
    $outline-width-value: if(
      custom-properties.is-custom-prop($outline-width),
      custom-properties.get-fallback($outline-width),
      $outline-width
    );
    // TODO(b/194792044): uncouple padding from outline-width
    // Note: Adjust padding to maintain consistent width with non-outlined buttons
    $padding-value: math.max($padding - $outline-width-value, 0);
    $padding-icon-value: math.max($padding-icon - $outline-width-value, 0);

    @include button-shared-theme.horizontal-padding(
      $padding: $padding-value,
      $padding-icon: $padding-icon-value,
      $query: $query
    );

    @include feature-targeting.targets($feat-structure) {
      @include theme.property(border-width, $outline-width);
    }

    #{button-ripple.$ripple-target} {
      @include feature-targeting.targets($feat-structure) {
        @include theme.property(top, -1 * $outline-width-value);
        @include theme.property(left, -1 * $outline-width-value);
        @include theme.property(bottom, -1 * $outline-width-value);
        @include theme.property(right, -1 * $outline-width-value);
        @include theme.property(border-width, $outline-width);
      }
    }
    .mdc-button__touch {
      @include feature-targeting.targets($feat-structure) {
        @include theme.property(
          left,
          'calc(-1 * outline-width)',
          $replace: (outline-width: $outline-width)
        );
        @include theme.property(
          width,
          'calc(100% + 2 * outline-width)',
          $replace: (outline-width: $outline-width)
        );
      }
    }
  }
}

///
/// Sets the outline color to the given color. This mixin should be
/// wrapped in a selector that qualifies button state.
/// @access private
///
@mixin _outline-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  @if $color {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(border-color, $color);
    }
  }
}
